package eudjok

class SetListController {

    static allowedMethods = [save: "POST", update: "POST", delete: "POST"]

    def index = {
        redirect(action: "list", params: params)
    }

    def list = {
        params.max = Math.min(params.max ? params.int('max') : 10, 100)
            [setListInstanceList: SetList.list(params), setListInstanceTotal: SetList.count()]
	}
	
	def listSL = {
		if(params.chave != "null"){
			def setList = Musica.findAllByDjAndTituloIlike(session.dj, "%"+params.chave+"%") 
			render(template:"/common/perfil/homePlaylist", model:[playList:musica])
		}		
	
	}
	
	def top = {
       def resul = AvaliacaoConsolidada.createCriteria().list(max:5, sort:'visitas', order:'desc') {
			eq('tipo','SetList')
		}
      		
		def res = AvaliacaoConsolidada.createCriteria().list(max:10, sort:'avaliacaoPositiva', order:'desc') {
			eq('tipo','SetList')
		}
    		
		def resultado = AvaliacaoConsolidada.createCriteria().list(max:5, sort:'indicacoes', order:'desc') {
			eq('tipo','SetList')
		}
        [setListInstanceIndicado: resultado, setListInstanceTotal: SetList.count(),setListInstanceVisity: resul,setListInstanceTotal: SetList.count(),setListInstanceTopList: res, setListInstanceTotal: SetList.count()]
       
    }
	
    def saveYoutube = {
	 	println(params)
        def musicaInstance = new Musica(params)
        
        def estilo = Estilo.findById(1)
       
		musicaInstance.estilo = estilo       
        musicaInstance.dataCriacao = new Date()
        musicaInstance.ultimaAtualizacao = new Date()
        musicaInstance.dj = session.dj

        if (musicaInstance.save(flush: true)) {
            flash.message = "${message(code: 'default.created.message', args: [message(code: 'musica.label', default: 'Musica'), musicaInstance.id])}"
            redirect(controller:"setList", action: "buscar", id: musicaInstance.id)
        }
        else {
            render(view: "create", model: [musicaInstance: musicaInstance])
        }
    }
	
	def saveSL = {
        def musicaInstance = new Musica(params)
        def estilo = Estilo.findById(1)
		def set = SetList.get(params.setListId)
       
		musicaInstance.estilo = estilo       
        musicaInstance.dataCriacao = new Date()
        musicaInstance.ultimaAtualizacao = new Date()
        musicaInstance.dj = session.dj
		musicaInstance.setList = set

        if (musicaInstance.save(flush: true)) {
            flash.message = "${message(code: 'setlist.musica.adicionada.message', args: [ musicaInstance.titulo, set.titulo])}"
            redirect(action: "show", id: set.id)
			//redirect(controller:"youTube", action: "buscarSL",  params:[id: musicaInstance.id, setListId:params.setListId])
        }
        else {
            //render(controller:"youTube", view: "buscarSL", model: [musicaInstance: musicaInstance, setListId:params.setListId])
			render(controller:"setList", view: "show", model: [musicaInstance: musicaInstance, setListId:params.setListId])
        }
    }
	
    def create = {
		
		def setListInstance = new SetList()
        setListInstance.properties = params
        return [setListInstance: setListInstance]
    }

    def save = {
        def setListInstance = new SetList(params)
        
		setListInstance.dj = session.dj
		setListInstance.dataCriacao = new Date()
		setListInstance.ultimaAtualizacao = new Date()
		setListInstance.descricao = ""
		
		
		if (setListInstance.save(flush: true)) {
            flash.message = "${message(code: 'eudj.set.created.message', args: [message(code: 'setList.label', default: 'SetList')])}"
            redirect(action: "show", id: setListInstance.id)
			//redirect(action: "view", id: setListInstance.id)
        }
        else {
            render(view: "create", model: [setListInstance: setListInstance])
        }
    }

    def view = {
        
		setList: Musica.findAllByDj(session.dj, [sort:"id", order: "desc"])
		
		def setListInstance = SetList.get(params.id)
        if (!setListInstance) {
            flash.message = "${message(code: 'default.not.found.message', args: [message(code: 'setList.label', default: 'SetList'), params.id])}"
            redirect(action: "list")
        }
        else {
            [setListInstance: setListInstance]
        }
    }
	
	def show = {
        def setListInstance = SetList.get(params.id)
        if (!setListInstance) {
            flash.message = "${message(code: 'default.not.found.message', args: [message(code: 'setList.label', default: 'SetList'), params.id])}"
            redirect(action: "list")
        }
        else {
            setListInstance.visitas = setListInstance.visitas + 1
			setListInstance.save(flush: true)
			[setListInstance: setListInstance, playList:Musica.findAllBySetList(setListInstance)]
			
        }
    }
	
    def edit = {
        def setListInstance = SetList.get(params.id)
        if (!setListInstance) {
            flash.message = "${message(code: 'default.not.found.message', args:[message(code: 'setList.label',	default: 'SetList'), params.id])}"
            redirect(action: "list")
        }
        else {
            return [setListInstance: setListInstance]
        }
    }

    def update = {
        def setListInstance = SetList.get(params.id)
        if (setListInstance) {
            if (params.version) {
                def version = params.version.toLong()
                if (setListInstance.version > version) {
                    
                    setListInstance.errors.rejectValue("version", "default.optimistic.locking.failure", [message(code: 'setList.label', default: 'SetList')] as Object[], "Another user has updated this SetList while you were editing")
                    render(view: "edit", model: [setListInstance: setListInstance])
                    return
                }
            }
            setListInstance.properties = params
            if (!setListInstance.hasErrors() && setListInstance.save(flush: true)) {
                flash.message = "${message(code: 'set.updated.message', args: [message(code: 'setList.label', default: 'SetList'), setListInstance.id])}"
                redirect(action: "show", id: setListInstance.id)
            }
            else {
                render(view: "edit", model: [setListInstance: setListInstance])
            }
        }
        else {
            flash.message = "${message(code: 'default.not.found.message', args: [message(code: 'setList.label', default: 'SetList'), params.id])}"
            redirect(action: "list")
        }
    }

    def delete = {
        def setListInstance = SetList.get(params.id)
        if (setListInstance) {
            try {
                setListInstance.delete(flush: true)
                flash.message = "${message(code: 'default.deleted.message', args: [message(code: 'setList.label', default: 'SetList'), params.id])}"
                redirect(action: "list")
            }
            catch (org.springframework.dao.DataIntegrityViolationException e) {
                flash.message = "${message(code: 'default.not.deleted.message', args: [message(code: 'setList.label', default: 'SetList'), params.id])}"
                redirect(action: "show", id: params.id)
            }
        }
        else {
            flash.message = "${message(code: 'default.not.found.message', args: [message(code: 'setList.label', default: 'SetList'), params.id])}"
            redirect(action: "list")
        }
    }
	
	def buscarM = {
		
		redirect(controller: "youTube", action: "buscarSL")
		
	}
	
}
